package com.supermap.dao.impl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.supermap.commontypes.LocationInfo;
import com.supermap.commontypes.SampleInfo;
import com.supermap.dao.SampleDao;

public class SampleDaoImpl implements SampleDao {

    private static Logger logger = Logger.getLogger(SampleDaoImpl.class);

    private String samplePath;

    private String sampleLocationInfoPath;

    public SampleDaoImpl(String samplePath, String sampleLocationInfoPath) {
        this.samplePath = samplePath;
        this.sampleLocationInfoPath = sampleLocationInfoPath;
    }

    public List<SampleInfo> readSample() {
        Path samplePFile = Paths.get(samplePath);
        List<SampleInfo> sampleInfos = new ArrayList<SampleInfo>();
        BufferedReader br = null;
        try {
            br = Files.newBufferedReader(samplePFile, StandardCharsets.UTF_8);
            String line = null;
            while ((line = br.readLine()) != null) {
                String tmp[] = StringUtils.split(line, "=");
                SampleInfo info = new SampleInfo();
                info.setKey(tmp[0]);
                info.setValue(tmp[1]);
                sampleInfos.add(info);
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            throw new RuntimeException(e);
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
                throw new RuntimeException(e);
            }
        }
        return sampleInfos;
    }

    public void saveSample(Set<SampleInfo> sampleInfos) {
        Path path = Paths.get(samplePath);
        Path locationInfoPath = Paths.get(sampleLocationInfoPath);
        BufferedWriter sampleBw = null;
        BufferedWriter locationInfoBw = null;

        try {
            if (Files.notExists(path)) {
                Files.createFile(path);
            }
            if (Files.notExists(locationInfoPath)) {
                Files.createFile(locationInfoPath);
            }
            sampleBw = Files.newBufferedWriter(path, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
            locationInfoBw = Files.newBufferedWriter(locationInfoPath, StandardCharsets.UTF_8, StandardOpenOption.APPEND);
            Iterator<SampleInfo> iterator = sampleInfos.iterator();
            while (iterator.hasNext()) {
                SampleInfo sampleInfo = iterator.next();
                sampleBw.write(sampleInfo.getKey() + "=" + sampleInfo.getValue());
                sampleBw.newLine();
                List<LocationInfo> locations = sampleInfo.getLocation();
                String locationStr = "";
                for (LocationInfo location : locations) {
                    locationStr += location.getFileName() + " , " + location.getLineNum() + "; ";
                }
                locationInfoBw.write(sampleInfo.getKey() + " -------- " + locationStr);
                locationInfoBw.newLine();
            }
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            throw new RuntimeException(e);
        } finally {
            try {
                if (sampleBw != null) {
                    sampleBw.close();
                }
                if (locationInfoBw != null) {
                    locationInfoBw.close();
                }
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
                throw new RuntimeException(e);
            }
        }
    }

}
